//
//  QYGRechargeController.m
//  wawaji_ios
//
//  Created by 奇艺果 on 2017/12/26.
//  Copyright © 2017年 yiguo qi. All rights reserved.
//

#import "QYGRechargeController.h"
#import "QYGUserInfoModel.h"
#import "QYGRechargeDetailView.h"
#import "QYGNetRequestModel.h"
#import "QYGCoinRecordModel.h"

@interface QYGRechargeController ()

@property (nonatomic, strong) UILabel *accountLabel;
@property (nonatomic, strong) QYGRechargeDetailView *rechargeDetailView;
@end

@implementation QYGRechargeController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupUI];
    WS(weakSelf);
    [[SGUtil shareUtil] setRechargeBlock:^(BOOL isSuccess, QYGCoinListModel *coinListModel){
        [weakSelf.view makeToast:isSuccess ? @"支付成功！" :@"支付失败！" duration:1 position:[NSValue valueWithCGPoint:CGPointMake(SCREEN_WIDTH * 0.5, SCREEN_HEIGHT * 0.65)] ];
        if(isSuccess){
            weakSelf.userInfo.coin =  weakSelf.userInfo.coin + coinListModel.coin + coinListModel.coin_award;
            weakSelf.userInfo.coin_buy = weakSelf.userInfo.coin_buy + coinListModel.coin + coinListModel.coin_award;
            weakSelf.accountLabel.text = [NSString stringWithFormat:@"%ld", weakSelf.userInfo.coin];
        }
    }];
}
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self setData];
    self.accountLabel.text = [NSString stringWithFormat:@"%ld", self.userInfo.coin];
}
- (void)setUserInfo:(QYGUserInfoModel *)userInfo{
    _userInfo = userInfo;
    self.accountLabel.text = [NSString stringWithFormat:@"%ld", userInfo.coin];
}
- (void)setData{
    WS(weakSelf);
    [QYGNetRequestModel getCoinsListCompleted:^(NSArray *coinListArr) {
        [weakSelf rechargeWithCoinListArr:coinListArr];
    } WithErrorCodeBlock:^(NSError *error) {
    }];
}

- (void)rechargeWithCoinListArr:(NSArray *)coinListArr{
    if(!coinListArr.count){
        [self.view makeToast:@"没有数据"];
        return;
    }
    CGFloat h = ScaleW(138);
    if(coinListArr.count > 4){
        h = h + ScaleH(66) * 4.5;
    }else{
        h = h + ScaleH(66) * coinListArr.count;
    }
    self.rechargeDetailView = [[QYGRechargeDetailView alloc] initWithFrame:CGRectMake(ScaleW(15), ScaleW(85), SCREEN_WIDTH - ScaleW(30),  h)];
    self.rechargeDetailView.coinListArr = coinListArr;
    [self.view addSubview:self.rechargeDetailView];
    WS(weakSelf);
    [self.rechargeDetailView setRechargeCompletBlock:^(BOOL isSuccess, QYGCoinListModel *coinListModel) {
        [weakSelf.view makeToast:isSuccess ? @"支付成功！" :@"支付失败！" duration:1 position:[NSValue valueWithCGPoint:CGPointMake(SCREEN_WIDTH * 0.5, SCREEN_HEIGHT * 0.65)] ];
        if(isSuccess){
            weakSelf.userInfo.coin =  weakSelf.userInfo.coin + coinListModel.coin + coinListModel.coin_award ;
            weakSelf.userInfo.coin_buy = weakSelf.userInfo.coin_buy + coinListModel.coin + coinListModel.coin_award;
            weakSelf.accountLabel.text = [NSString stringWithFormat:@"%ld", weakSelf.userInfo.coin];
        }
    }];
}
#pragma mark - setUI
- (void)setupUI{
    self.view.backgroundColor = [UIColor qyg_mainColor];
    self.navigationItem.title = @"我的金币";
    [self.view addSubview:self.accountLabel];
    [self.accountLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view.mas_top).offset(ScaleW(16));
        make.centerX.equalTo(self.view.mas_centerX);
    }];
    
}


#pragma mark - lazy
- (UILabel *)accountLabel{
    if(!_accountLabel){
        _accountLabel = [UILabel labelWithText:@"100" setFont:ScaleBoldFont(40) setTextColor:[UIColor colorWithHex:@"ffda64"] setTextAlignment:NSTextAlignmentCenter];
    }
    return _accountLabel;
}
@end
